<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" :clearable="true" />
      </el-form-item>
      <el-form-item label="机构序列" prop="institutionalSequence">
        <el-select
          v-model="queryParams.institutionalSequence"
          placeholder="请输入机构序列"
          :clearable="true"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.UNIT_INSTITUTIONAL_SEQUENCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['unit:unit:create']" @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="unit"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item
          label="修改"
          @click="handleMenuUpdate(row)"
          v-hasPermi="['unit:unit:update']"
        />
      </template>
    </Table>
  </ContentWrap>
  <UnitEdit ref="editFormRef" @success="getPage" />
</template>
<script setup name="Unit" lang="ts">
import { DICT_TYPE, getDictObj, getIntDictOptions } from '@/utils/dict'
import * as UnitApi from '@/api/unit/unit'
import UnitEdit from './UnitEdit.vue'
const editFormRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  institutionalSequence: undefined
})
const queryData = ref()
const columns = [
  {
    label: '名称',
    prop: 'name'
  },
  {
    label: '机构序列',
    prop: 'institutionalSequence',
    formatter: ({ institutionalSequence }) =>
      getDictObj(DICT_TYPE.UNIT_INSTITUTIONAL_SEQUENCE, institutionalSequence).label
  },
  {
    label: '机构规格',
    prop: 'institutionalSpecifications',
    formatter: ({ institutionalSpecifications }) =>
      getDictObj(DICT_TYPE.UNIT_INSTITUTIONAL_SPECIFICATIONS, institutionalSpecifications).label
  },
  {
    label: '机构类别',
    prop: 'institutionalCategory',
    formatter: ({ institutionalCategory }) =>
      getDictObj(DICT_TYPE.UNIT_INSTITUTIONAL_CATEGORY, institutionalCategory).label
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  },
  {
    label: '更新时间',
    prop: 'updateTime'
  }
]
/** 初始化 */
onMounted(() => {
  getPage()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await UnitApi.pageQuery(queryParams.value)
}

/** 处理table行双击事件 */
const handleTableDblclick = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 处理右击事件 修改 */
const handleMenuUpdate = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 新增按钮操作 */
const handleClickCreate = () => {
  editFormRef.value.open('create', 0)
}
</script>
